﻿using System;
using System.Collections.Generic;


namespace Ext.Net.UIS
{

    public class TermTag
    {
        public class List : List<TermTag>{}

        public TermTag(string name)
        {
            if (name == null)
                throw new ArgumentNullException("name");
            Name = name;
        }

        public string Name { get; set; }
        public bool IsNamespace { get; set; }

        public override string ToString()
        {
            return "Tag " + Name;
        }

        public virtual void AddTo(Term term) { term.Tags.Add(this); }
        public virtual bool IsAddedTo(Term term) { return term.Tags.Contains(this); }
    }


    #region Cached tags

    public class EntityTag : TermTag
    {
        public EntityTag(string name = null) : base(name ?? "Entity") { IsNamespace = true; }
        public override void AddTo(Term term) { term._IsEntity = true; base.AddTo(term); }
        public override bool IsAddedTo(Term term) { return term._IsEntity; }
    }

    public class GroupTag : TermTag
    {
        public GroupTag(string name = null) : base(name ?? "Group") { }
        public override void AddTo(Term term) { term._IsGroup = true; base.AddTo(term); }
        public override bool IsAddedTo(Term term) { return term._IsGroup; }
    }

    public class LabelTag : TermTag
    {
        public LabelTag(string name = null) : base(name ?? "Label") { }
        public override void AddTo(Term term) { term._IsLabel = true; base.AddTo(term); }
        public override bool IsAddedTo(Term term) { return term._IsLabel; }
    }

    public class RowTag : TermTag
    {
        public RowTag(string name = null) : base(name ?? "Row") { }
        public override void AddTo(Term term) { term._IsRow = true; base.AddTo(term); }
        public override bool IsAddedTo(Term term) { return term._IsRow; }
    }

    public class UnionTag : TermTag
    {
        public UnionTag(string name = null) : base(name ?? "Union") { }
        public override void AddTo(Term term) { term._IsUnion = true; base.AddTo(term); }
        public override bool IsAddedTo(Term term) { return term._IsUnion; }
    }

    #endregion


    public static class TermTags
    {
        public static TermTag Composite = new TermTag("Composite") { IsNamespace = true };
        public static TermTag Entity = new EntityTag();
        public static TermTag Group = new GroupTag("Group");
        public static TermTag Label = new LabelTag("Label");
        public static TermTag Row = new RowTag();
        public static TermTag Union = new UnionTag();

        public static TermTag ID = new TermTag("ID");
        public static TermTag Name = new TermTag("Name");
        public static TermTag Main = new TermTag("Main");
        public static TermTag Utility = new TermTag("Utility");
        public static TermTag Header = new TermTag("Header");

        public static TermTag Simple = new TermTag("Simple");
        public static TermTag Small = new TermTag("Small");
    }


    partial class Term
    {
        internal protected bool _IsComposite, _IsEntity, _IsGroup, _IsLabel, _IsRow, _IsUnion;

        public bool IsComposite() { return _IsComposite; }
        public bool InComposite() { return Parent != null && Parent._IsComposite; }
        public void MakeComposite() { Add(TermTags.Composite); }

        public bool IsEntity() { return _IsEntity; }
        public bool InEntity() { return Parent != null && Parent._IsEntity; }
        public void MakeEntity() { Add(TermTags.Entity); }

        public bool IsGroup() { return _IsGroup; }
        public bool InGroup() { return Parent != null && Parent._IsGroup; }
        public void MakeGroup() { Add(TermTags.Group); }

        public bool IsID() { return Have(TermTags.ID); }
        public void MakeID() { Add(TermTags.Label); }

        public bool IsLabel() { return _IsLabel; }
        public void MakeLabel() { Add(TermTags.Label); }

        public bool IsRow() { return _IsRow; }
        public bool InRow() { return Parent != null && Parent._IsRow; }
        public void MakeRow() { Add(TermTags.Row); }

        public bool IsUnion() { return _IsUnion; }
        public bool InUnion() { return Parent != null && Parent._IsUnion; }
        public void MakeUnion() { Add(TermTags.Union); }
    }


    partial class TermNode
    {
        public bool IsComposite() { return Term != null && Term._IsComposite; }
        public bool InComposite() { return Term != null && Term.Parent != null && Term.Parent._IsComposite; }
        public void MakeComposite() { Add(TermTags.Composite); }

        public bool IsEntity() { return Term != null && Term._IsEntity; }
        public bool InEntity() { return Term != null && Term.Parent != null && Term.Parent._IsEntity; }
        public void MakeEntity() { Add(TermTags.Entity); }

        public bool IsGroup() { return Term != null && Term._IsGroup; }
        public bool InGroup() { return Term != null && Term.Parent != null && Term.Parent._IsGroup; }
        public void MakeGroup() { Add(TermTags.Group); }

        public bool IsID() { return Have(TermTags.ID); }
        public void MakeID() { Add(TermTags.Label); }

        public bool IsLabel() { return Term != null && Term._IsLabel; }
        public void MakeLabel() { Add(TermTags.Label); }

        public bool IsRow() { return Term != null && Term._IsRow; }
        public bool InRow() { return Term != null && Term.Parent != null && Term.Parent._IsRow; }
        public void MakeRow() { Add(TermTags.Row); }

        public bool IsUnion() { return Term != null && Term._IsUnion; }
        public bool InUnion() { return Term != null && Term.Parent != null && Term.Parent._IsUnion; }
        public void MakeUnion() { Add(TermTags.Union); }
    }


}
